export const useAchievementAwardStore = defineStore('achievementAward', () => {
  const allForm = ref({})
  const DeptList = ref([])
  const UserList = ref([])
  const eduList = ref([
    {
      label: '博士',
      value: '0',
    },
    {
      label: '硕士',
      value: '1',
    },
    {
      label: '学士',
      value: '2',
    },
  ])
  function addForm(obj) {
    try {
      if (!obj || typeof obj !== 'object') {
        throw new Error('Invalid input: obj must be an object')
      }

      // 验证 allForm.value 是否为对象
      if (!allForm.value || typeof allForm.value !== 'object') {
        throw new Error('Invalid state: allForm.value must be an object')
      }

      // 安全性检查：过滤掉潜在的危险属性
      const safeObj = {}
      for (const key in obj) {
        if (obj.hasOwnProperty(key)) {
          // 这里可以根据具体需求添加更多的安全性检查
          if (
            typeof obj[key] === 'string' ||
            typeof obj[key] === 'number' ||
            typeof obj[key] === 'boolean'
          ) {
            safeObj[key] = obj[key]
          }
        }
      }

      // 根据键是否存在进行修改或添加
      for (const key in safeObj) {
        if (safeObj.hasOwnProperty(key)) {
          if (allForm.value.hasOwnProperty(key)) {
            // 如果 allForm.value 中存在该键，则修改值
            console.log('修改键值对', key)

            allForm.value[key] = safeObj[key]
          } else {
            // 如果 allForm.value 中不存在该键，则添加键值对
            allForm.value = { ...allForm.value, [key]: safeObj[key] }
          }
        }
      }
    } catch (error) {
      console.error('Error adding or updating form:', error.message)
    }
  }

  function setForm(obj) {
    allForm.value = Object.assign({}, obj)
  }

  function resetForm() {
    allForm.value = {}
  }

  interface Item {
    label: string
    value: string | number
  }

  function setDeptList(arr: Array<Item>) {
    DeptList.value = arr
  }
  function setUserList(arr: Array<Item>) {
    UserList.value = arr
  }

  return {
    allForm,
    DeptList,
    UserList,
    eduList,
    addForm,
    setForm,
    resetForm,
    setDeptList,
    setUserList,
  }
})
